package com.springbootblog.controller.fontend.thread.async;

public class TestDie
{
    // 资源1
    private static Object resource1 = new Object();
    // 资源2
    private static Object resource2 = new Object();

    public static void main(String[] args)
    {
        // 定义一个线程
        Thread thread1 = new Thread(() -> {
            // 锁住 资源1
            synchronized (resource1) {
                System.out.println("Thread 1: Holding resource 1...");

                // 线程延迟
                try{ Thread.sleep(100); }catch (InterruptedException e){}
                System.out.println("Thread 1: Waiting for resource 2...");
            }
            // 锁住资源2（这个时候资源2被下边的线程锁着呢）
            synchronized (resource2)
            {
                System.out.println("Thread 1: Acquired resource 2!");
            }
        });

        // 定义一个线程
        Thread thread2 = new Thread(() -> {
            // 锁住资源2
            synchronized (resource2)
            {
                System.out.println("Thread 2: Holding resource 2...");

                // 线程延迟
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                System.out.println("Thread 2: Waiting for resource 1...");
            }
            // 锁住资源1（这个时候资源1被上边的线程锁着呢）
            synchronized (resource1)
            {
                System.out.println("Thread 2: Acquired resource 1!");
            }
        });
        // 执行线程
        thread1.start();
        thread2.start();
    }
}
